﻿#include "cyclic_reception.h"
#include "admin_function.h"
#include "Upload_database.h"
#include "heart_beat.h"

// 处理新的客户端接收线程
void *cyclic_reception(void *arg)
{
    int listenfd = *(int *)arg;
    printf("已经进入新的线程%ld号中---\n", pthread_self() % 100);
    pump_list(listenfd);
    printf("新线程正在等待新的客户端请求---\n");
    int ret = 0;
    int number = 0;
    while (1)
    {
        char new_buf[1024] = {0};
        memset(new_buf, 0, sizeof(new_buf));
        ret = recv(listenfd, new_buf, 1024, 0);
        if (ret > 0)
        {
            // 正常处理消息并更新心跳时间
            handle_client_message(listenfd, new_buf);
        }
        printf("新线程接收到数据---%s\n", new_buf);
        if (ret == -1)
        {
            perror("recv");
        }

        if (ret == 0)
        {
            printf("客户端已经关闭---\n");
        }

        // 解析数据
        data_unpack(new_buf, number);
        number++;
    }
}

// 新处理函数并上传数据库
void data_unpack(const char *buf, int number)
{
    // 初始化一个 DAT 结构体变量
    DAT dat;
    memset(&dat, 0, sizeof(DAT));

    // 解析 JSON 字符串
    cJSON *json = cJSON_Parse(buf);
    if (json == NULL)
    {
        printf("解析JSON为空\n");
    }

    cJSON *node = NULL;

    node = cJSON_GetObjectItem(json, "temperature");
    if (node == NULL)
    {
        printf("can't find temperature\n");
        exit(-1);
    }
    if (node->type == cJSON_Number)
    {
        dat.temperature = node->valuedouble;
    }

    node = cJSON_GetObjectItem(json, "liquid_level");
    if (node == NULL)
    {
        printf("can't find liquid_level\n");
        exit(-1);
    }
    if (node->type == cJSON_Number)
    {
        dat.liquid_level = node->valuedouble;
    }

    node = cJSON_GetObjectItem(json, "flow_rate");
    if (node == NULL)
    {
        printf("can't find flow_rate\n");
        exit(-1);
    }
    if (node->type == cJSON_Number)
    {
        dat.flow_rate = node->valuedouble;
    }

    node = cJSON_GetObjectItem(json, "pump_id");
    if (node == NULL)
    {
        printf("can't find pump_id\n");
        exit(-1);
    }
    if (node->type == cJSON_Number)
    {
        dat.pump_id = node->valueint;
    }

    node = cJSON_GetObjectItem(json, "valve_state");
    if (node == NULL)
    {
        printf("can't find valve_state\n");
        exit(-1);
    }
    if (node->type == cJSON_Number)
    {
        dat.valve_state = node->valueint;
    }

    node = cJSON_GetObjectItem(json, "negative_pressure");
    if (node == NULL)
    {
        printf("can't find negative_pressure\n");
        exit(-1);
    }
    if (node->type == cJSON_Number)
    {
        dat.negative_pressure = node->valuedouble;
    }

    node = cJSON_GetObjectItem(json, "pressure");
    if (node == NULL)
    {
        printf("can't find pressure\n");
        exit(-1);
    }
    if (node->type == cJSON_Number)
    {
        dat.pressure = node->valuedouble;
    }

    node = cJSON_GetObjectItem(json, "time");
    if (node == NULL)
    {
        printf("can't find time\n");
        exit(-1);
    }
    if (node->type == cJSON_String)
    {
        strcpy(dat.time, node->valuestring);
    }

    // 释放 JSON 对象占用的内存
    cJSON_Delete(json);

    // 把数据上传到mysql
    int flag = 0;
    MYSQL *connect = NULL;
    connect = connect_mysql();
    flag = data_insert_database4(&dat, connect);
    if (flag == 0)
    {
        printf("数据写入失败\n");
    }
    else
    {
        printf("第%d条数据写入数据库成功\n", number);
    }
    mysql_close(connect);
}